package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.model.SelectedObjectModel;
	import com.tongji.housediy.utils.PopUpAdmin;
	import com.tongji.housediy.view.components.RoomBorder;
	import com.tongji.housediy.view.components.RoomFloor;
	import com.tongji.housediy.view.components.RoomShape;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.TitleWindowBoundsEvent;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class WallWindowMediator extends Mediator
	{
		[Inject]
		public var view:WallTitleWindow;
		
		[Inject]
		public var selectedModel:SelectedObjectModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		[Embed(source="assets/images/warning.png")]
		private var BulletCritical:Class;
		
		override public function onRegister():void
		{
			view.addEventListener(TitleWindowBoundsEvent.WINDOW_MOVING, windowMovingHandler);
			
			view.deleteBtn.addEventListener(MouseEvent.CLICK, deleteBtnClickHandler);
			view.colorBtn.addEventListener(MouseEvent.CLICK, colorBtnClickHandler);
		}
		
		private function windowMovingHandler(event:TitleWindowBoundsEvent):void
		{
			dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.UPDATE_LINE_POSITION, 0, 0, view.x + view.width / 2, view.y + view.height / 2));
		}
		
		private function deleteBtnClickHandler(event:MouseEvent):void
		{
			var alert:Alert;
			alert = Alert.show("确定要删除吗？", "删除直墙?", Alert.OK | Alert.CANCEL, view.stage as Sprite, alertClickHandler);
			alert.titleIcon = BulletCritical;
		}
		
		private function alertClickHandler(event:CloseEvent):void
		{
			if (event.detail == Alert.OK)
			{
				selectedModel.deleteSelectedObj();
			}
		}
		
		private function colorBtnClickHandler(event:MouseEvent):void
		{
			var selectedObj:RoomShape = selectedModel.selectedObj as RoomShape;
			if (selectedModel.selectedObj is RoomFloor)
			{
				PopUpAdmin.popupCenterWindow("floorColor", view, mediatorMap);
				goodsModel.arrangeSizes(selectedObj.hostGoods, "地面色彩");
			}
			else
			{
				PopUpAdmin.popupCenterWindow("wallColor", view, mediatorMap);
				goodsModel.arrangeSizes(selectedObj.hostGoods, "墙面色彩");
				goodsModel.arrangeMaps((selectedObj as RoomBorder).wallMap, (selectedObj as RoomBorder).skirtingMap, null);
			}
		}
	}
}